<template>
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
        v-model="userAccount"
        name="userAccount"
        label="账号"
        placeholder="请输入您的账号"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="userPassword"
        type="password"
        name="userPassword"
        label="密码"
        placeholder="请输入您的密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
    </van-cell-group>
    <div style="margin: 16px">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
    <div style="margin: 16px">
      <van-button round @click="doLogon" block type="primary" native-type="submit">
        注册
      </van-button>
    </div>
  </van-form>
</template>

<script setup lang="ts">
import { ref } from "vue";
import axiosUrl from "../plugins/myAxios";
import { useRouter,useRoute } from "vue-router";
import { showSuccessToast, showFailToast } from "vant";

const router = useRouter();
const route = useRoute(); 

const userAccount = ref();
const userPassword = ref();

const doLogon = () => {
  router.push({
    path:'/user/logon'
  })
}

const onSubmit = async () => {
  const res = await axiosUrl.post("/user/login", {
    userAccount: userAccount.value,
    userPassword: userPassword.value,
  });
  console.log(res, "用户登录");
  if (res.code === 0 && res.data) {
    showSuccessToast("登录成功");
    const redirectUrl = route.query?.redirect as string ?? '/';
    window.location.href = redirectUrl;
  } else {
    showFailToast("登录失败");
  }
};
</script>

<style>
</style>